#include <GL/GL.h>
#include <GL/GLUT.h>
#include "textura.h"
#include "colisao.h"
#include "bola.h"
#include <math.h>

using namespace std;

Ball::Ball(int ID,int Texture)
{
	_ID_ = ID;
	_Tex_Index_ = Texture;
	_Radius_ = 0.0;
	_Pos_X_ = 0.0;
	_Pos_Y_ = 0.0;
	_Pos_Z_ = 0.0;
	_Speed_ = 0.0;
	_Atrito_ = 0.01;
	_Dead_ = false;

	// quadratic things to draw our sphere with textures
	_Quad_Sphere_=gluNewQuadric();
	gluQuadricNormals(_Quad_Sphere_, GLU_SMOOTH);
	gluQuadricTexture(_Quad_Sphere_, GL_TRUE);

	_Quad_Shadow_ = gluNewQuadric();
	gluQuadricNormals(_Quad_Sphere_, GLU_SMOOTH);
	gluQuadricTexture(_Quad_Sphere_, GL_TRUE);
}
Ball::Ball(int ID,int Texture,float PosX,float PosY, float PosZ, float Radius)
{
	_ID_ = ID;
	_Tex_Index_ = Texture;
	_Radius_ = Radius;
	_Pos_X_ = PosX;
	_Pos_Y_ = PosY;
	_Pos_Z_ = PosZ;
	_Speed_ = 0.0;
	_Atrito_ = 0.01;
	_Dead_ = false;

	// quadratic things to draw our sphere with textures
	_Quad_Sphere_=gluNewQuadric();
	gluQuadricNormals(_Quad_Sphere_, GLU_SMOOTH);
	gluQuadricTexture(_Quad_Sphere_, GL_TRUE);

	_Quad_Shadow_ = gluNewQuadric();
	gluQuadricNormals(_Quad_Sphere_, GLU_SMOOTH);
	gluQuadricTexture(_Quad_Sphere_, GL_TRUE);
}
Ball::~Ball()
{
	// todo: check vector, maybe ?
	//delete(_Quad_Sphere_);
}
void Ball::Hit(float PosX,float PosZ,float Speed)
{
	//pega as difs em X e Z para identificar qual quadrante do plano XZ esta o taco
	float difX = PosX - _Pos_X_;
	float difZ = PosZ - _Pos_Z_;

	// gera o primeiro next move
	_Direction_.X = difX;
	_Direction_.Z = difZ;
	_Speed_ = Speed;
}
void Ball::Hit(int ID_Bola_Ativa,float PosX,float PosZ,float Speed)
{
	_ID_Batida = ID_Bola_Ativa;
	Hit(PosX,PosZ,Speed);
}

// Desenha bola
void Ball::Draw()
{
	if(_Speed_ > 0.001)
	{
		int		hit_target;

		if((hit_target = Hit_Ball(_ID_,_ID_Batida,_Pos_X_,_Pos_Z_,_Radius_,_Speed_)) >= 0)
		{
			Hit(_Pos_X_,_Pos_Z_,_Speed_ * 0.4);// faz uma nova "batida", com apenas 40% da forca
		}
		else if((hit_target = Hit_Cacapas(_Pos_X_,_Pos_Z_,_Radius_)) >= 0)
		{
			_Speed_ = _Atrito_;
			_Direction_.X = 100;
			_Direction_.Z = 100;
			_Pos_Y_ = 10.0;//faz a bola sumir de vista
			_Dead_ = false;
		}
		else if((hit_target = Hit_Borders(_Pos_X_,_Pos_Z_,_Radius_)) >= 0)
		{
			switch(hit_target)
			{
				case 0:
				case 1://bati numa borda dos lados
				{
					_Direction_.X = -_Direction_.X;
					//_Direction_.Z = -_Direction_.Z;
				}
				break;
				case 2:
				case 3://bati na borda de cima/baixo
				{
					//_Direction_.X = -_Direction_.X;
					_Direction_.Z = -_Direction_.Z;
				}
				break;
			}

			_Speed_ -= 0.1*_Speed_ ;//perde 10% da velocidade ao bater numa parede
		}
		//se move ativamente
		_Pos_X_			-= _Speed_ * (_Direction_.X / 100);
		_Pos_Z_			-= _Speed_ * (_Direction_.Z / 100);

		_Speed_ -= _Atrito_;

		if((_Angle_+=_Speed_) > 360)
			_Angle_ = 0;
	}
	else
	{
		_Speed_ = 0.0;
	}

	//desenha eu mesma
	Draw(_Pos_X_,_Pos_Y_,_Pos_Z_);

}

// Desenha bola
void Ball::Draw(float X,float Y,float Z)
{
	glPushMatrix();

	Switch_Texture(_Tex_Index_);
	Begin_Texture(_Tex_Index_);

	glTranslatef(X,Y,Z);
	glRotatef(_Angle_,1.0,0,0);
	gluSphere(_Quad_Sphere_, _Radius_ , 24 , 24 );//just create in origin...

	glPopMatrix( );
}

// Desenha sombra da bola
void Ball::DrawShadow()
{
	glPushMatrix( );

		Switch_Texture(2);
		Begin_Texture(2);

		glTranslatef(_Pos_X_,_Pos_Y_-_Radius_-0.1,_Pos_Z_);

		glRotatef(90,1.0,0,0);

		gluDisk(_Quad_Shadow_,0.0,_Radius_+0.01,24,1);


	glPopMatrix( );
}
void Ball::DrawShadow(float LightX,float LightY,float LightZ)
{
	float difX = LightX - _Pos_X_;
	float difZ = LightZ - _Pos_Z_;
	float stepX = 0.05;
	float stepZ = 0.05;

	glPushMatrix( );

		Switch_Texture(2);
		Begin_Texture(2);
		//if(difX > 0 && difZ > 0)
		//	glTranslatef(_Pos_X_ - stepX ,_Pos_Y_-_Radius_,_Pos_Z_ - stepZ);
		//else if(difX > 0 && difZ < 0)
		//	glTranslatef(_Pos_X_ - stepX ,_Pos_Y_-_Radius_,_Pos_Z_ + stepZ);
		//else if(difX < 0 && difZ > 0 )
		//	glTranslatef(_Pos_X_ + stepX ,_Pos_Y_-_Radius_,_Pos_Z_ - stepZ);
		//else if(difX < 0 && difZ < 0 )
		//	glTranslatef(_Pos_X_ + stepX ,_Pos_Y_-_Radius_,_Pos_Z_ + stepZ);
		//else
			glTranslatef(_Pos_X_,_Pos_Y_-_Radius_,_Pos_Z_);

		glRotatef(90,1.0,0,0);

		gluDisk(_Quad_Shadow_,0.0,_Radius_+0.02,24,1);

	glPopMatrix( );

	//DrawShadow();
}
